|
|
|
|
|
|
|
You may redimension an array variable as many times as you want during the course of a program. However, each time you do, all the values that were in the array before are reinitialized to the default value. In other words, none of the values in the array are preserved. |
|
|
|
|
|
|
|
|
Preserving Array Contents |
|
|
|
|
|
|
|
|
Sometimes you might want to preserve the old array contents but just add on a new element. To preserve the contents of the array elements, use the Preserve keyword with the ReDim statement, like this: |
|
|
|
 |
|
|
|
|
Private Sub cmdPreserve_Click()
' Preserve any previous values ReDim Preserve maboolBonus(4) As Boolean
' Add the new value
maboolBonus(4) = True
' Print Bonus Flags
Me.Cls
Me.Print maboolBonus(0)
Me.Print maboolBonus(1)
Me.Print maboolBonus(2)
Me.Print maboolBonus(3)
Me.Print maboolBonus(4)
End Sub |
|
|
|
|
|
|
|
|
When you use the Preserve keyword, Visual Basic creates a whole new array in memory to the new size, copies all the values from the old array to the new one, and then deletes the old array. This way, all your old values are preserved, but youve now added room for any new items to the end of the array. Of course, this assumes you have increased the size of the array. You may also decrease the size of the array while still preserving the contents of the array. If you decrease the size, any elements at the end of the array are simply dropped from the new array. Because Visual Basic has to create a new array every time you use the Preserve keyword, you want to be judicious in your use of this commandthis is a very time-consuming and memory-hungry operation. |
|
|
|
|
|